package com.shiwaixiangcun.customer.module.property

import android.graphics.Color
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import com.google.gson.reflect.TypeToken
import com.jaeger.recyclerviewdivider.RecyclerViewDivider
import com.lzy.okgo.model.HttpParams
import com.shiwaixiangcun.customer.GlobalApi
import com.shiwaixiangcun.customer.R
import com.shiwaixiangcun.customer.adapter.AdapterProperty
import com.shiwaixiangcun.customer.base.BaseActivity
import com.shiwaixiangcun.customer.entity.CommonDataList
import com.shiwaixiangcun.customer.entity.MyPropertyEntity
import com.shiwaixiangcun.customer.network.NetworkHelper
import com.shiwaixiangcun.customer.ui.activity.ResidentCertificationActivity
import com.shiwaixiangcun.customer.utils.JsonUtil
import kotlinx.android.synthetic.main.activity_my_property.*
import kotlinx.android.synthetic.main.top_bar_blue.*
import me.bakumon.statuslayoutmanager.library.OnStatusChildClickListener
import me.bakumon.statuslayoutmanager.library.StatusLayoutManager

class MyPropertyActivity : BaseActivity(), View.OnClickListener {

    private var mHeaderView: View? = null
    var mAdapter: AdapterProperty? = null
    private var mDataList: MutableList<MyPropertyEntity>? = null

    private var mNetworkHelper: NetworkHelper? = null
    private var mTvPropertyNumber: TextView? = null
    private var statusLayoutManager: StatusLayoutManager? = null
    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.back_left -> {
                finish()
            }
//            R.id.tv_top_right -> {
//                readyGo(ManagePropertyActivity::class.java)
//            }
            R.id.btn_add_property -> {
                val bundle = Bundle()
                bundle.putString("type", "add")
                bundle.putInt("fromActivity", 0x0010)
                readyGo(ResidentCertificationActivity::class.java, bundle)
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_my_property)
        initHeaderView()
        initEmptyView()
        initViewAndEvent()
        initData()

    }

    override fun onRestart() {
        super.onRestart()
        initData()
    }

    private fun initEmptyView() {
        val emptyView = LayoutInflater.from(mContext).inflate(R.layout.layout_empty, null, false)
        emptyView.findViewById<TextView>(R.id.tv_empty).text = "你还没有添加房产"
        statusLayoutManager = StatusLayoutManager.Builder(rv_property)
                .setDefaultLayoutsBackgroundColor(Color.WHITE)
                .setEmptyLayout(emptyView)
                .setErrorLayout(R.layout.layout_error)
                .setOnStatusChildClickListener(object : OnStatusChildClickListener {
                    override fun onErrorChildClick(p0: View?) {
                        initData()
                    }

                    override fun onEmptyChildClick(p0: View?) {
                    }

                    override fun onCustomerChildClick(p0: View?) {
                    }
                })
                .build()


    }

    private fun initData() {
        val httpParams = HttpParams()

        mNetworkHelper?.getRequestWithToken(GlobalApi.getPropertyList, true, httpParams, object : NetworkHelper.NetWorkHelperBack {
            override fun requestSuccess(json: String) {
                val type = object : TypeToken<CommonDataList<MyPropertyEntity>>() {}.type
                val fromJson = JsonUtil.fromJson<CommonDataList<MyPropertyEntity>>(json, type)
                when {
                    fromJson == null -> showToastShort("获取失败")
                    fromJson.success -> when (fromJson.responseCode) {
                        1001 -> {
                            if (fromJson.data.isEmpty()) {
                                statusLayoutManager?.showEmptyLayout()
                                tv_top_right.visibility = View.GONE
                            } else {
                                tv_top_right.visibility = View.GONE
                                statusLayoutManager?.showSuccessLayout()
                                mDataList?.clear()
                                mDataList?.addAll(fromJson.data)
                                mTvPropertyNumber?.text = mDataList?.size.toString()
                                mAdapter?.removeAllHeaderView()
                                mAdapter?.addHeaderView(mHeaderView)
                                mAdapter?.notifyDataSetChanged()
                            }
                        }
                        else -> {
                            showToastShort(fromJson.message)
                        }
                    }
                    else -> {
                        statusLayoutManager?.showEmptyLayout()
                        tv_top_right.visibility = View.GONE
                    }

                }
                Log.e(BUG_TAG, json)
            }

            override fun requestFail() {
            }
        })

    }

    /**
     * 初始化头部信息
     */
    private fun initHeaderView() {
        mHeaderView = LayoutInflater.from(mContext).inflate(R.layout.header_property, null, false)
        mTvPropertyNumber = mHeaderView?.findViewById(R.id.tv_property_number)
    }

    /**
     * 初始化视图
     */
    private fun initViewAndEvent() {

        tv_page_name.text = "我的房产"
//        tv_top_right.text = "管理"
//        tv_top_right.setTextColor(resources.getColor(R.color.word_green))
//        tv_top_right.visibility = View.GONE
//        tv_top_right.setOnClickListener(this)
        back_left.setOnClickListener(this)
        btn_add_property.setOnClickListener(this)
        mNetworkHelper = NetworkHelper(mContext, supportFragmentManager)
        val divider = RecyclerViewDivider.Builder(mContext)
                .setOrientation(RecyclerViewDivider.VERTICAL)
                .setStyle(RecyclerViewDivider.Style.END)
                .setStartSkipCount(1)
                .setMarginLeft(34f)
                .setMarginRight(16f)
                .setSize(0.5f)
                .setColorRes(R.color.color_divider_0_1)
                .build()
        mDataList = ArrayList()
        mAdapter = AdapterProperty(mDataList)
        rv_property.addItemDecoration(divider)
        rv_property.layoutManager = LinearLayoutManager(this)
        rv_property.adapter = mAdapter


    }
}
